home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / HLPMANAG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.8 KB  |  290 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.7  $
  6. //
  7. // Implementation of class THelpFileManager & THelpContext
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_HLPMANAG_H)
  11. # include <owl/hlpmanag.h>
  12. #endif
  13. #if !defined(OWL_APPLICAT_H)
  14. # include <owl/applicat.h>
  15. #endif
  16. #if !defined(OWL_WINDOW_H)
  17. # include <owl/window.h>
  18. #endif
  19. #if !defined(OWL_FRAMEWIN_H)
  20. # include <owl/framewin.h>
  21. #endif
  22.  
  23. OWL_DIAGINFO;
  24.  
  25. //
  26. // Constructor saves the name of the help file and creates the global
  27. // context table.
  28. //
  29. THelpFileManager::THelpFileManager(const string& helpFileName)
  30. {
  31.   SetHelpFile(helpFileName);
  32.   ContextTable = new TContextList;
  33. }
  34.  
  35. //
  36. // Deletes the allocated context table.
  37. //
  38. THelpFileManager::~THelpFileManager()
  39. {
  40.   delete ContextTable;
  41. }
  42.  
  43. //
  44. // Called by EvHelp() to activate the help file with the help context id.
  45. //
  46. void
  47. THelpFileManager::ActivateHelp(TWindow* /*window*/, int helpFileContextId)
  48. {
  49. #if !defined(BI_COMP_MSC)
  50.   TApplication* app = TYPESAFE_DOWNCAST(this, TApplication);
  51.   if (app)
  52.     app->GetMainWindow()->WinHelp(GetHelpFile().c_str(), HELP_CONTEXT, helpFileContextId);
  53.   else
  54. #endif
  55.     ::WinHelp(0, GetHelpFile().c_str(), HELP_CONTEXT, helpFileContextId);
  56. }
  57.  
  58. //
  59. // Deactivates the help.
  60. //
  61. void
  62. THelpFileManager::DeactivateHelp()
  63. {
  64. #if !defined(BI_COMP_MSC)
  65.   TApplication* app = TYPESAFE_DOWNCAST(this, TApplication);
  66.   if (app)
  67.     app->GetMainWindow()->WinHelp(GetHelpFile().c_str(), HELP_QUIT, 0);
  68.   else
  69. #endif
  70.     ::WinHelp(0, GetHelpFile().c_str(), HELP_QUIT, 0);
  71. }
  72.  
  73. //
  74. // Changes the name of the help file.
  75. //
  76. void
  77. THelpFileManager::SetHelpFile(const string& helpFileName)
  78. {
  79.   HelpFileName = helpFileName;
  80. }
  81.  
  82. DEFINE_RESPONSE_TABLE1(THelpFileManager, TEventHandler)
  83.   EV_WM_HELP,
  84. END_RESPONSE_TABLE;
  85.  
  86. //
  87. // Event handler for the WM_HELP message.
  88. //
  89. void
  90. THelpFileManager::EvHelp(HELPINFO* hi)
  91. {
  92.   THelpContext context;
  93.   bool success = false;
  94.  
  95.   if (hi) {
  96.     if (hi->iContextType == HELPINFO_MENUITEM)
  97.       success = GetHelpContextFromMenu(context, hi->iCtrlId);
  98.     else if (hi->iContextType == HELPINFO_WINDOW)
  99.       success = GetHelpContextFromControl(context, hi->iCtrlId, (HWND)hi->hItemHandle);
  100.   }
  101.  
  102.   if (success)
  103.     ActivateHelp(context.GetWindow(), context.GetHelpFileContextId());
  104. }
  105.  
  106. //
  107. // Return true if the control id is the same as the context entry's control id.
  108. //
  109. static bool
  110. MatchControlId(const THelpContext context, int controlId, HWND hwnd)
  111. {
  112.   if (context.GetControlContextId() == controlId &&
  113.       context.GetWindow() && context.GetWindow()->IsChild(hwnd)) {
  114.     return true;
  115.   }
  116.   return false;
  117. }
  118.  
  119. //
  120. // Return true if a match for the control id was found.
  121. //
  122. bool
  123. THelpFileManager::GetHelpContextFromControl(THelpContext& context,
  124.                                             int controlId, HWND hwnd) const
  125. {
  126.   // for each item in the context table
  127.   //
  128.   for (TContextListIterator iterator(*ContextTable); iterator; iterator++) {
  129.     context = iterator.Current();
  130.  
  131.     // try to match the control id
  132.     //
  133.     if (IsIndirectHelpContext(context)) {
  134.       THelpContext* indirect = (THelpContext*)context.GetWindow();
  135.       while (!IsLastIndirectContext(*indirect)) {
  136.         if (MatchControlId(*indirect, controlId, hwnd)) {
  137.           context = *indirect;
  138.           return true;
  139.         }
  140.         indirect++;
  141.       }
  142.     }
  143.     else if (MatchControlId(context, controlId, hwnd))
  144.       return true;
  145.   }
  146.   return false;
  147. }
  148.  
  149. //
  150. // Return true if the menu item identifier matches a context entry's menu
  151. // item identifier.
  152. //
  153. static bool
  154. MatchMenuId(const THelpContext context, int menuId)
  155. {
  156.   if (context.GetMenuContextId() == menuId) {
  157.     return true;
  158.   }
  159.   return false;
  160. }
  161.  
  162. //
  163. // Return true if a match for the menu item id was found.
  164. //
  165. bool
  166. THelpFileManager::GetHelpContextFromMenu(THelpContext& context, int menuId) const
  167. {
  168.   // for each item in the context table
  169.   //
  170.   for (TContextListIterator iterator(*ContextTable); iterator; iterator++) {
  171.     context = iterator.Current();
  172.  
  173.     // try to match the menu item identifier
  174.     //
  175.     if (IsIndirectHelpContext(context)) {
  176.       THelpContext* indirect = (THelpContext*)context.GetWindow();
  177.       while (!IsLastIndirectContext(*indirect)) {
  178.         if (MatchMenuId(*indirect, menuId)) {
  179.           context = *indirect;
  180.           return true;
  181.         }
  182.         indirect++;
  183.       }
  184.     }
  185.     else if (MatchMenuId(context, menuId))
  186.       return true;
  187.   }
  188.   return false;
  189. }
  190.  
  191. //
  192. // Add an entry into the global context table.
  193. //
  194. void
  195. THelpFileManager::AddContextInfo(TWindow* window, int helpId, int menuId, int controlId)
  196. {
  197.   ContextTable->Add(THelpContext(window, helpId, menuId, controlId));
  198. }
  199.  
  200. static int
  201. WindowsEqual(const THelpContext& context, void* ptr)
  202. {
  203.   TWindow* targetWindow = (TWindow*)ptr;
  204.   if (context.GetWindow() == targetWindow)
  205.     return 1;
  206.   return 0;
  207. }
  208.  
  209. //
  210. // Remove all the entries in the context table for the window.
  211. //
  212. void
  213. THelpFileManager::RemoveContextInfo(TWindow* window)
  214. {
  215.   THelpContext* context = ContextTable->FirstThat(WindowsEqual, window);
  216.   while (context != 0) {
  217.     ContextTable->Detach(*context);
  218.     context = ContextTable->FirstThat(WindowsEqual, window);
  219.   }
  220.  
  221.   if (ContextTable->GetItemsInContainer() == 0) {
  222.     DeactivateHelp();
  223.   }
  224. }
  225.  
  226. //
  227. // Default constructor that initializes every data member to zero.
  228. //
  229. THelpContext::THelpContext()
  230. :
  231.   HelpFileContextId(0),
  232.   MenuContextId(0),
  233.   ControlContextId(0),
  234.   Window(0)
  235. {
  236. }
  237.  
  238. //
  239. // Convenient constructor to initialize the context entry with the
  240. // proper ids.
  241. //
  242. THelpContext::THelpContext(TWindow* window, int helpId, int menuId, int controlId)
  243. :
  244.   Window(window),
  245.   HelpFileContextId(helpId),
  246.   MenuContextId(menuId),
  247.   ControlContextId(controlId)
  248. {
  249. }
  250.  
  251. //
  252. // Make a copy of the context entry.
  253. //
  254. THelpContext::THelpContext(const THelpContext& other)
  255. {
  256.   *this = other;
  257. }
  258.  
  259. //
  260. // Do nothing.
  261. //
  262. THelpContext::~THelpContext()
  263. {
  264. }
  265.  
  266. //
  267. // Make a copy of the context entry.
  268. //
  269. THelpContext&
  270. THelpContext::operator =(const THelpContext& other)
  271. {
  272.   HelpFileContextId = other.GetHelpFileContextId();
  273.   MenuContextId     = other.GetMenuContextId();
  274.   ControlContextId  = other.GetControlContextId();
  275.   Window            = other.GetWindow();
  276.   return *this;
  277. }
  278.  
  279. //
  280. // Return true if the context entries match.
  281. //
  282. int
  283. THelpContext::operator ==(const THelpContext& other) const
  284. {
  285.   return Window == other.GetWindow() &&
  286.          HelpFileContextId == other.GetHelpFileContextId() &&
  287.          MenuContextId == other.GetMenuContextId() &&
  288.          ControlContextId == other.GetControlContextId();
  289. }
  290.